﻿<%@ Page Language="C#" MasterPageFile="~/Manager.master" AutoEventWireup="true" CodeFile="ProductImages.aspx.cs" Inherits="Manager_Product_ProductImages" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
    <div align="left">
        <table style="width: 100%" border="1" cellpadding="2" cellspacing="2" class="tbl_Comon-Search" id="tblBodyMain">
            <tr>
                <td align="left" class="td_Control" colspan="2">
                    <asp:ValidationSummary ID="ValidationSummary1" runat="server" ValidationGroup="grpAdd" />
                </td>
            </tr>
            <tr>
                <td class="td_Title" style="width: 95px" valign="middle">
                    <asp:Label ID="Label1" runat="server" Text="Chọn Hình ảnh"></asp:Label></td>
                <td align="left" class="td_Control">
                    <table border="0" cellpadding="2" cellspacing="0" width="100%">
                        <tr>
                            <td align="left">
                                <div id="divFile" runat="server">
                                    <asp:DataList ID="dtlDetailFile" runat="server" OnItemDataBound="dtlDetailFile_ItemDataBound1">
                                        <ItemTemplate>
                                            <table border="0" cellpadding="2" cellspacing="0">
                                                <tr>
                                                    <td>
                                                        <asp:HyperLink ID="hplLink" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"ImgPD_Url") %>'
                                                            NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"ImgPD_Url") %>' onclick='return false;'  ToolTip='<%# DataBinder.Eval(Container.DataItem,"ImgPD_Desc") %>' onmouseover="OpenPopUpTooltip(this)" onmouseout="HidePopUpTooltip()" ></asp:HyperLink>
                                                            
                                                    </td>
                                                    <td>
                                                        <a href="#">
                                                            <img id="imgDel" onclick='fnDelFile(this)' link='<%# DataBinder.Eval(Container.DataItem,"ImgPD_Url") %>'
                                                                iddd='<%# DataBinder.Eval(Container.DataItem,"ImgPD_ID") %>' src="../../Images/Icon/cmdDelete.gif"
                                                                style="cursor: cursor;" border="0" /></a>
                                                    </td>
                                                </tr>
                                            </table>
                                        </ItemTemplate>
                                    </asp:DataList>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td align="right">
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <input id="btnAdd" class="cssButton" type="button" value="Thêm file" onclick="AddNewFile()" />
                                <asp:DataList ID="dtlFile" runat="server" CellPadding="0" CellSpacing="0" Width="100%">
                                    <ItemTemplate>
                                        <asp:Label ID="lblIndex" Text="<%#Container.ItemIndex%>" runat="server" Style="visibility: hidden"></asp:Label>
                                        <table border="0" cellpadding="2" cellspacing="0" width="100%">
                                            <tr>
                                                <td>
                                                    <asp:TextBox ID="txtName" runat="server" Style="background-color: WhiteSmoke; border: solid 1px DarkGray;
                                                        height: 16px" Text="" Width="160px"></asp:TextBox>
                                                </td>
                                                <td nowrap="nowrap">
                                                    <asp:FileUpload ID="FileUpd" runat="server" />
                                                    <font color="blue"><b>or</b> </font>
                                                </td>
                                                <td width="70%">
                                                    <asp:TextBox ID="txtLink" runat="server" Text="http://" Style="background-color: Seashell;
                                                        border: solid 1px DarkGray; height: 16px" Width="95%" onblur="javascript:if(this.value==''){this.value='http://';};"
                                                        onfocus="javascript:if(this.value=='http://'){this.value='';};"></asp:TextBox>
                                                </td>
                                                <td width="10%">
                                                    <a href="#">
                                                        <img id="imgDel" runat="server" onclick="fnDelTableRow(this)" src="../../Images/Icon/cmdDelete.gif"
                                                            style="cursor: cursor;" border="0" /></a>
                                                </td>
                                            </tr>
                                        </table>
                                    </ItemTemplate>
                                </asp:DataList></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td class="cssManage_left" colspan="2">
                </td>
            </tr>
            <tr>
                <td style="width: 95px">
                </td>
                <td style="height: 24px" align="left">
                    <input id="btnSave" class="cssButton" type="button" value="Bảo lưu" onclick="fnSaveFile()" />
                    <asp:Button ID="btnCancel" runat="server" CssClass="cssButton" Text="Trở về" OnClick="btnCancel_Click"
                        Width="80px" CausesValidation="False" /></td>
            </tr>
        </table>
    </div>
    
    <div class='product_popup' id="divTooltip" onmouseover="OpenDivPopUpTooltip()" onmouseout="HidePopUpTooltip()"
        onclick="HidePopUpTooltip()" style="cursor:pointer">
        <table class="tbl_popup" cellpadding="0" cellspacing="0">
            <tr>
                <td>
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr>
                            <td class="td_Middle_L_Pop">
                            </td>
                            <td class="td_Middle_C_Pop" align="left" id="tdProductTitle" style="color:White">
                            </td>
                            <td class="td_Middle_R_Pop">
                            </td>
                        </tr>
                    </table>
                </td>
            </tr>
            <tr>
                <td nowrap="nowrap" rowspan="2" class="product_detail_popup" id="tdProductDetail"
                    style="text-align: center">
                </td>
            </tr>
        </table>
    </div>
    <input type="hidden" id="hImg" name="hImg" runat="server" />
    <input type="hidden" id="hFileName" name="hFileName" runat="server" />
    <input type="hidden" id="hFile" name="hFile" runat="server" />
    <input id="hLink" runat="server" name="hLink" type="hidden" />
    <input id="hMethod" runat="server" name="hMethod" type="hidden" />

    <script language="javascript" type="text/javascript">
    String.prototype.trim = function () {
		return this.replace(/^\s*/, "").replace(/\s*$/, "");
	}
	
     function fnDelFile(obj)
     {
        if(confirm('Delete???'))
        {
            var IdDd= obj.getAttribute("IdDd");   
            var Link=obj.getAttribute("Link");
            var IdD='<%=Request.QueryString["PDID"]%>';
            Manager_Product_ProductImages.AjaxDelFile(IdDd,Link,IdD,AjaxReturn)
        }
     }
     function AjaxReturn(res)
     {
        if(res!=undefined)
        {
            var divFile=document.getElementById("<%= divFile.ClientID%>") ;
            var dtl=res.value;
            divFile.innerHTML=dtl;
        }
     }
     function AddNewFile()
     {
        var tblMaster=document.getElementById("<%=dtlFile.ClientID%>");       
       
		    //-------
            var tr=tblMaster.insertRow(tblMaster.rows.length);		
				//
				var td=document.createElement('td');
				tr.appendChild(td);						
				var tableFIle=tblMaster.rows[0].cells[0];  				      
				td.innerHTML= tableFIle.innerHTML;			
				tblMaster.rows[tblMaster.rows.length-1].cells[0].children[0].innerHTML=tblMaster.rows.length-1;			
				
				 var tbl=document.getElementById("tblBodyMain"); 
                 parent.fnFullFrame(tbl.clientHeight);
				
     }
     function fnDelTableRow(objImg)
	 {
	    var index=objImg.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.children[0].innerHTML;
	    var tblMaster=document.getElementById("<%=dtlFile.ClientID%>");
	    if(tblMaster.rows.length<=1)
	    {
	        alert('Không thể xóa thêm'); return;
	    }
	    if(confirm('Delete???')) tblMaster.deleteRow(index);	    
	    for(var i=0;i<tblMaster.rows.length;i++)
	    {
	        tblMaster.rows[i].cells[0].children[0].innerHTML=i;
	    }
	 }
	 function fnSaveFile()
	 {
	    
	    
	    var tblMaster=document.getElementById("<%=dtlFile.ClientID%>");
	    var hFile=document.getElementById("<%=hFile.ClientID%>");
	    var hFileName=document.getElementById("<%=hFileName.ClientID%>");	    
	    var hLink=document.getElementById("<%=hLink.ClientID%>");	    	    
	    hFile.value="";
	    hFileName.value="";
	    hLink.value="";
	      
        for(var i=0;i<tblMaster.rows.length;i++)
        {
           var txtName=tblMaster.rows[i].cells[0].children[1].rows[0].cells[0].children[0];	 
           var objFile=tblMaster.rows[i].cells[0].children[1].rows[0].cells[1].children[0]; 
           var txtLink=tblMaster.rows[i].cells[0].children[1].rows[0].cells[2].children[0];     
                       	       
            var sFile=objFile.value; 
            if(sFile.trim()=="" && txtLink.value=="http://" || txtLink.value=="") 
           {
                alert('Chọn Link hoặc file để upload') ;
       	        txtLink.focus();
                return;  
           }  

            var iLength=parseInt(sFile.length);
            var Index=parseInt(sFile.lastIndexOf("\\"));
            var file=sFile.substr(Index+1,iLength);   
            if(i<tblMaster.rows.length-1)
	        {
		        hFile.value+=file+"＊"; 
		        hFileName.value+=txtName.value+"＊";    
		        hLink.value+=txtLink.value+"＊";
	        }
	        else
	        {
		        hFile.value+=file; 
		        hFileName.value+=txtName.value;    
		        hLink.value+=txtLink.value;
	        }	
        }
       
	    var theForm=document.forms["aspnetForm"];

		var hMethod=document.getElementById("<%= hMethod.ClientID%>");
		hMethod.value="Save_Click";
		theForm.submit();

	 }
	
	
	 function OpenPopUpTooltip(obj)
     {         
       var tdProductDetail=document.getElementById("tdProductDetail");    
       var tdProductTitle=document.getElementById("tdProductTitle");           
       var divTooltip=document.getElementById("divTooltip");
       divTooltip.style.visibility="visible" ;
       var sleft=ObjectPosition(obj)[0];      
       var stop=ObjectPosition(obj)[1]-(divTooltip.clientHeight/2); 
       divTooltip.style.left = sleft-obj.clientWidth+50 + 'px';
       divTooltip.style.top =  stop-obj.clientHeight+ 'px';   
      // tdProductDetail.innerHTML="<div style='padding:200 200 200 200'><img src='http://localhost:1038/WebShop/Images/Icon/loadingPD.gif' border='0'></div>";
       var sSrc;
        if (obj.href.indexOf("http://") >= 0) 
            sSrc=obj.href;
        else
              sSrc="../../ProductIMG/"+obj.href;
            


	   var Index=parseInt(sSrc.lastIndexOf("/"));
	   var iLength=parseInt(sSrc.length);
	   var filename=sSrc.substr(Index+1,iLength);

       if(filename=='Noimages.gif')
       {
            divTooltip.style.visibility="hidden" ;
            return;
       }
       var sTenSP=obj.title;          
        tdProductTitle.innerHTML=sTenSP; 
        var img="<img src="+sSrc+">"  
          
        tdProductDetail.innerHTML=img;
      //var img="<img src="+sSrc+" width="+sWidth+" height="+sHeight+">"       
     }
     function OpenEventPopUpTooltip(e,lbl)
     { 

       obj=lbl.nextSibling ;
       var sSrc=obj.src;       
       var divTooltip=document.getElementById("divTooltip");
        divTooltip.style.visibility="visible" ;    
       var sleft=ObjectPosition(obj)[0];    
        var stop=ObjectPosition(obj)[1]-(divTooltip.clientHeight/2);        
      // var stop=ObjectPosition(obj)[1]-100;   
    //alert(clientHeight)
    //alert(obj.clientHeight);
       
	   var Index=parseInt(sSrc.lastIndexOf("/"));
	   var iLength=parseInt(sSrc.length);
	   var filename=sSrc.substr(Index+1,iLength);

       if(filename=='Noimages.gif')
       {
            divTooltip.style.visibility="hidden" ;
            return;
       }

        var tdProductDetail=document.getElementById("tdProductDetail");    
        var tdProductTitle=document.getElementById("tdProductTitle");           
              
        
      	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
		if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	
		var leftPos = e.clientX + 20;
		if(leftPos<0)leftPos = 0;
        divTooltip.style.left = leftPos + 'px';
        divTooltip.style.top =  stop+ 'px';    
       // divTooltip.style.top = e.clientY + 'px';
             
        
        tdProductTitle.innerHTML=lbl.innerHTML;
       // var img="<img src="+sSrc+" width="+sWidth+" height="+sHeight+">"  
        var img="<img src="+sSrc+">";
        
        tdProductDetail.innerHTML=img;
        //divTooltip.style.top = e.clientY - obj.offsetHeight- (divTooltip.clientHeight/2) + st + 'px';
     }    
     
     function HidePopUpTooltip()
     { 
             var divTooltip=document.getElementById("divTooltip");
             divTooltip.style.visibility="hidden"
     }
     
     function OpenDivPopUpTooltip()
     { 
             var divTooltip=document.getElementById("divTooltip");
             divTooltip.style.visibility="visible"
     }
     
     
     function ObjectPosition(obj) 
     {
          var curleft = 0;
          var curtop = 0;
          if (obj.offsetParent) {
                do {
                      curleft += obj.offsetLeft;
                      curtop += obj.offsetTop;
                } while (obj = obj.offsetParent);
          }
          return [curleft,curtop];
	}
    </script>

</asp:Content>